import { PropType } from 'vue';
import {
  TApprovalTaskItem,
  TApprovalStartActivity,
  TApprovalEndActivity,
  TApprovalHistoryData
} from '@/components/business/Approval/approval-helper';
// import { formatDateToString } from '@numa/common/utils/date';
import { useDateFormat } from '@vueuse/core';

export type TItemData = TApprovalTaskItem | TApprovalStartActivity | TApprovalEndActivity;
import { TaskNode } from './TaskNode';
import { StartNode } from './StartNode';
import { EndNode } from './EndNode';
export type TTaskStatus = 'INIT' | 'DOING' | 'DONE'; // 进行中 | 已完成

export type TProgressTaskType = 'start' | 'task' | 'end';
export const approvalDateFormat = (date: string) => {
  // return formatDateToString(date, 'YYYY/MM/DD hh:mm');
  return date? useDateFormat(new Date(date), 'YYYY/MM/DD HH:mm').value : '';
};

export const isTaskActivity = (type: TProgressTaskType, data?: TItemData): data is TApprovalTaskItem => {
  return type === 'task';
};
export const isEndActivity = (type: TProgressTaskType, data?: TItemData): data is TApprovalEndActivity => {
  return type === 'end';
};
export const isStartActivity = (type: TProgressTaskType, data?: TItemData): data is TApprovalStartActivity => {
  return type === 'start';
};

export const ProgressItem = defineComponent({
  props: {
    data: {
      type: Object as PropType<TItemData | undefined>,
      default: () => {}
    },
    type: {
      type: String as PropType<TProgressTaskType>, // 提交任务 | 进行中 & 已完成审批 | 结果任务
      default: 'start'
    },
    // 审批是否都已经全部完成
    isApproveFinish: {
      type: Boolean,
      default: false
    },
    sourceData: {
      type: Object as PropType<TApprovalHistoryData>,
      default: null
    }
  },
  render() {
    if (isStartActivity(this.type, this.data)) {
      return <StartNode data={this.data} />;
    }
    if (isTaskActivity(this.type, this.data)) {
      return <TaskNode data={this.data} />;
    }
    return <EndNode data={this.data} sourceData={this.sourceData} isApproveFinish={this.isApproveFinish} />;
  }
});
